#include <iostream>
#include "OperationMode_CBC.hpp"

OperationMode_CBC::OperationMode_CBC(Algorithm &system) : OperationMode(system) {
	R = Block(system.getBlockSize(), 0xff);
}

OperationMode_CBC::OperationMode_CBC(Algorithm &system, const Block &iv) : OperationMode(system) {
	R = iv;
}

Block OperationMode_CBC::encrypt(const Block &plaintext) {

	Block plainXORold = plaintext;

	plainXORold = plaintext ^ R;

	Block ciphertext = system.encrypt(plainXORold);

	R = ciphertext;

	return ciphertext;
}

Block OperationMode_CBC::decrypt(const Block &ciphertext) {

	Block plaintext = system.decrypt(ciphertext);
	
	plaintext ^= R;
	
	R = ciphertext;

	return plaintext;
}
